/*
 * @Description  : Scroll
 * @Autor        : 杜康
 * @Date         : 2021-08-14 10:11:10 +0800
 * @LastEditors  : 杜康
 * @LastEditTime : 2021-08-14 13:45:17 +0800
 */

/**
 * @description: 获取滚动条距顶部的距离
 * @param {*}
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 10:14:16 +0800
 */
const getScrollTop = () => {
    return (
        (document.documentElement && document.documentElement.scrollTop) ||
        document.body.scrollTop
    );
};

/**
 * @description: 在${duration}时间内，滚动条平滑滚动到${to}指定位置
 * @param { Number } to
 * @param { Number } duration
 * @return {*}
 * @author: 杜康
 * @Date: 2021-08-14 10:14:25 +0800
 */
const scrollTo = (to, duration) => {
    if (duration <= 0) {
        setScrollTop(to);
        return;
    }
    let diff = to - getScrollTop();
    if (diff === 0) return;
    let step = (diff / duration) * 10;
    requestAnimationFrame(() => {
        if (Math.abs(step) > Math.abs(diff)) {
            setScrollTop(getScrollTop() + diff);
            return;
        }
        setScrollTop(getScrollTop() + step);
        if (
            (diff > 0 && getScrollTop() >= to) ||
            (diff < 0 && getScrollTop() <= to)
        ) {
            return;
        }
        scrollTo(to, duration - 16);
    });
};

/**
 * @description: 设置滚动条距顶部的距离
 * @param { Number } value
 * @return { Number }
 * @author: 杜康
 * @Date: 2021-08-14 10:14:47 +0800
 */
const setScrollTop = (value) => {
    // scrollTo(x, y);
    // document.documentElement.scrollTop
    window.scrollTo(0, value);
    return value;
};

/**
 * @description: 获取一个元素的距离文档(document)的位置，类似JQ中的offset()
 * @param { HTMLElement } ele
 * @return { {left: number, top: number} }
 * @author: 杜康
 * @Date: 2021-08-14 10:15:12 +0800
 */
const offset = (ele) => {
    let pos = {
        left: 0,
        top: 0,
    };
    while (ele) {
        pos.left += ele.offsetLeft;
        pos.top += ele.offsetTop;
        ele = ele.offsetParent;
    }
    return pos;
};

// 兼容 <IE10
if (!Date.now) {
    Date.now = () => {
        return new Date().getTime();
    };
}
(() => {
    "use strict";
    let vendors = ["webkit", "moz"];
    for (let i = 0; i < vendors.length && !window.requestAnimationFrame; ++i) {
        let vp = vendors[i];
        window.requestAnimationFrame = window[vp + "RequestAnimationFrame"];
        window.cancelAnimationFrame =
            window[vp + "CancelAnimationFrame"] ||
            window[vp + "CancelRequestAnimationFrame"];
    }
    if (
        /iP(ad|hone|od).*OS 6/.test(window.navigator.userAgent) || // iOS6 is buggy
        !window.requestAnimationFrame ||
        !window.cancelAnimationFrame
    ) {
        let lastTime = 0;
        window.requestAnimationFrame = (callback) => {
            let now = Date.now();
            let nextTime = Math.max(lastTime + 16, now);
            return setTimeout(() => {
                callback((lastTime = nextTime));
            }, nextTime - now);
        };
        window.cancelAnimationFrame = clearTimeout;
    }
})();

export default {
    getScrollTop,
    offset,
    scrollTo,
    setScrollTop,
};
